// GameObject.cpp //
#include "GameObject.h"
#include "Particle.h"
//Inherents from object, adds anilities that game objects all share,
// these will include players, projectiles, and enemies, and include
// functions like take damage, explode, etc. Basically objects
// that affect gameplay will inherent from this.

GameObject::GameObject(string keyname) : Object(keyname) //constructor
{
	health = 0;
	flickerInterval = 0.0f;
	CollisionList = new vector<GameObject*>;
}

GameObject::~GameObject() //destuctor
{
	delete CollisionList;
}

// check objects against those in its collision list
// this is done so we don't have to check every object against every object
void GameObject::Update(float deltaTime)
{
    for(vector<GameObject*>::iterator Iter = CollisionList->begin(); Iter != CollisionList->end(); ++Iter)
        if(CheckCollision(this, (*Iter)))
            Collide((*Iter));

	if(flickerInterval > 0) // sprite will flickr
	    flickerInterval -= deltaTime;
	else
	    sprite->SetTextureIndex(0); // stops flicker
	    
	Object::Update(deltaTime);

	RemoveOffScreen();
}

void GameObject::TakeDamage(int amount) // when something is hurt
{
	if(health > 0)
	{
		health -= amount;

		if(health <= 0) // kill!
		{
			Explode();
		    Remove();
		}
		else
		{
			sprite->SetTextureIndex(1);
			flickerInterval = DamageEffectTime; // flicker when hit
		}
	}
}

// activate the explosion, some classes override this
void GameObject::Explode() 
{
	if(explosionKeyname != "")
	    Particle* explosion = new Particle(explosionKeyname, position);
}

// check if the object bounds are intersecting with the viewable screen
// kill if it's all gone
void GameObject::RemoveOffScreen()
{
	SDL_Rect bounds = {0, 0, ScreenWidth, ScreenHeight};
	SDL_Rect intersect = Intersection(GetBounds(), bounds);

	if(intersect.w == 0 && intersect.h == 0)
	    Remove();
}

// grabs a random object from a vector, was going to be used in multiplayer
// to choose target
GameObject* GameObject::PickRandomObject(vector<GameObject*>* PickList)
{
	int listSize = PickList->size();
	if(listSize > 0)
	    return (*PickList)[rand() % listSize];
	else
	    return NULL;
}
